var _api = _interopRequireDefault(require("../../../../utils/api")), _eventEmitter = _interopRequireDefault(require("../../../../public/utils/event-emitter")), _contactUtil = require("../../../../utils/contact-util"), _overview = _interopRequireDefault(require("./overview")), _ai = _interopRequireDefault(require("./ai")), _rank = _interopRequireDefault(require("./rank"));

function _interopRequireDefault(e) {
    return e && e.__esModule ? e : {
        default: e
    };
}

function _toConsumableArray(e) {
    return _arrayWithoutHoles(e) || _iterableToArray(e) || _nonIterableSpread();
}

function _nonIterableSpread() {
    throw new TypeError("Invalid attempt to spread non-iterable instance");
}

function _iterableToArray(e) {
    if (Symbol.iterator in Object(e) || "[object Arguments]" === Object.prototype.toString.call(e)) return Array.from(e);
}

function _arrayWithoutHoles(e) {
    if (Array.isArray(e)) {
        for (var t = 0, a = new Array(e.length); t < e.length; t++) a[t] = e[t];
        return a;
    }
}

var CHILD_TABS_INDEX = {
    OVERVIEW: 0,
    AI: 1,
    RANK: 2
}, CHILD_MENU = [ "分析总揽", "智能分析", "排行榜" ], ISHIGHER = 1, overview = (0, _overview.default)(), ai = (0, 
_ai.default)(), rank = (0, _rank.default)(), PAGES = [ overview, ai, rank ], LIMIT = 30;

Component({
    properties: {
        card: Object,
        isMember: Boolean,
        isMasses: Boolean,
        BAR_HEIGHT: String
    },
    attached: function() {
        this.TABS = [];
    },
    ready: function() {
        this.initEvent(), this.init();
    },
    methods: {
        resetTab: function() {
            this.TABS.length = 0;
        },
        initEvent: function() {
            var t = this;
            [ [ "radar.department.change", function(e) {
                t.setCat(e), wx.setTimeout(function() {
                    t.resetTab(), t.getData(t.data.childIndex);
                }, 200);
            } ] ].forEach(function(e) {
                return _eventEmitter.default.addListener.apply(_eventEmitter.default, _toConsumableArray(e));
            });
        },
        init: function() {
            var e = this;
            this.initData(), this.loadCategory().then(function() {
                return e.getData(CHILD_TABS_INDEX.OVERVIEW);
            });
        },
        initData: function() {
            var e = CHILD_MENU;
            this.data.isMasses && (e = CHILD_MENU.slice(0, 2)), this.setData({
                childIndex: CHILD_TABS_INDEX.OVERVIEW,
                CHILD_TABS_INDEX: CHILD_TABS_INDEX,
                CHILD_MENU: e,
                department: {},
                searchAnalyze: {
                    inputShowed: !1
                }
            });
        },
        onPullDownRefresh: function() {
            PAGES[this.data.childIndex].onPullDownRefresh();
        },
        onReachBottom: function() {
            var e = this.data, t = e.childIndex;
            if (e.searchAnalyze.inputShowed) {
                if (this.noMore) return;
                this.loadSearchMember();
            } else PAGES[t].onReachBottom();
        },
        loadCategory: function() {
            var e = this.data.card, t = Promise.resolve({
                catid: 0
            });
            return _contactUtil.AUTH.NORMAL !== e.auth ? t = Promise.resolve({
                catid: _contactUtil.ROOT_CATID,
                name: "所有成员"
            }) : e.isLeader && (t = _api.default.minicard.category.lists({
                data: {
                    guid: e.guid,
                    ishigher: ISHIGHER,
                    cardid: e.cardid
                }
            }).then(function(e) {
                return e.data[0];
            }).catch(function() {
                return {
                    catid: 0
                };
            })), t.then(this.setCat.bind(this));
        },
        setCat: function(e) {
            this.setData({
                department: e
            });
        },
        getData: function(e) {
            this.onChildChange({
                currentTarget: {
                    dataset: {
                        index: e
                    }
                }
            });
        },
        onChildChange: function(e) {
            var t = e.currentTarget.dataset.index, a = Number(t);
            this.setData({
                childIndex: a
            }), this.TABS.includes(a) || (this.TABS.push(a), PAGES[a].init(this), PAGES[a].loadData(!0));
        },
        onAnalyzeSearch: function(e) {
            var t = e.detail.value;
            this.setData({
                "searchAnalyze.keyword": t
            }), this.loadSearchMember(!0);
        },
        tiggerAnalyzeSearch: function(e) {
            var t = e.detail.inputShowed;
            this.setData({
                "searchAnalyze.inputShowed": t
            });
        },
        onAnalyzeChange: function(e) {
            var t = e.detail.value;
            overview.chnageAnalyzeIndex(+t);
        },
        onTrendChange: function(e) {
            var t = e.detail.value;
            overview.changeTrendIndex(+t);
        },
        onAiChange: function(e) {
            var t = e.detail.value;
            ai.changeAiIndex(+t);
        },
        onRankChange: function(e) {
            var t = e.detail.value;
            rank.changeRankIndex(+t);
        },
        gotoSingleView: function(e) {
            var t = e.currentTarget.dataset, a = t.index, i = t.childindex, n = this.data, r = n.childIndex;
            n.isMasses || PAGES[r].gotoSingleView(a, i);
        },
        onSelectCat: function() {
            var e = this.data, t = e.card, a = e.department, i = e.isMasses, n = e.isMember, r = t.guid, o = t.cardid;
            i || wx.navigateTo({
                url: "/pages/radar/department/department?type=analyze&guid=".concat(r, "&ishigher=").concat(n ? 1 : 0, "&cardid=").concat(o, "&catid=").concat(a.catid)
            });
        },
        loadSearchMember: function(e) {
            var a = this;
            e && (this.offset = 0), _api.default.minicard.manage.searchmember({
                data: {
                    limit: LIMIT,
                    guid: this.data.card.guid,
                    offset: this.offset || 0,
                    keyword: this.data.searchAnalyze.keyword
                }
            }).then(function(e) {
                var t = e.data.list;
                a.setData({
                    searchAnalyzeLists: t
                }), a.noMore = t.length < LIMIT, a.offset += LIMIT;
            });
        },
        viewDetailAnalyze: function(e) {
            var t = e.currentTarget.dataset.index, a = this.data.searchAnalyzeLists[t], i = getCurrentPages()[0];
            i.$put("card.data", a), i.$route("/pages/radar/singleuser/singleuser");
        }
    }
});